package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Site;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("SiteController")
public class SiteController extends CmiEmaController {

	@RequestMapping("/listSites")
	public ModelAndView listSites(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = sitePaginationService.updatePagination(request, pagination);
		Set<Site> sites = sitePaginationService.getPaginationSites(pagination);

		mav.addObject("sites", sites);
		mav.addObject("entityName", "site");
		mav.addObject("pagination", pagination);
		mav.setViewName("site/listSites");
		return mav;
	}

	public String listSites() {
		return "redirect:/listSites";
	}

	@RequestMapping("/viewSite")
	public ModelAndView viewSite(@RequestParam Integer siteId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("site", siteDAO.findSiteByPrimaryKey(siteId));
		mav.setViewName("site/viewSite");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteSites")
	public ModelAndView confirmDeleteSites(
			@RequestParam(value = "deleteSitesIds", required = true) Set<Integer> deleteSitesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Site> deleteSites = siteDAO.findSitesByIds(deleteSitesIds);
		Set<Site> referencedSites = siteService.loadReferenceSites(deleteSites);
		boolean hasReferenced;
		if (referencedSites.size() == 0) {
			mav.addObject("deleteEntitys", deleteSites);
			hasReferenced = false;
		} else {
			deleteSites.removeAll(referencedSites);
			mav.addObject("referencedEntitys", referencedSites);
			mav.addObject("deleteEntitys", deleteSites);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Site");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listSites");
		return mav;
	}

	@RequestMapping("/deleteSite")
	public String deleteSite(@RequestParam Integer siteId) {
		Site site = siteDAO.findSiteByPrimaryKey(siteId);
		siteService.deleteSite(site);
		return "forward:/listSites";
	}

	@RequestMapping("/confirmDeleteSite")
	public ModelAndView confirmDeleteSite(@RequestParam Integer siteId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("site", siteDAO.findSiteByPrimaryKey(siteId));

		mav.setViewName("site/deleteSite");

		return mav;
	}

	@RequestMapping("/deleteSites")
	public String deleteSites(@RequestParam(value = "deleteSitesId", required = true) Set<Integer> deleteSitesId) {
		siteService.deleteSites(deleteSitesId);
		return "forward:/listSites";
	}

	@RequestMapping("/editSite")
	public ModelAndView editSite(@RequestParam Integer siteId) {

		ModelAndView mav = new ModelAndView();

		Site site = siteDAO.findSiteByPrimaryKey(siteId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("site", site);

		// 把所有的引用site对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(siteService.loadSiteObjectReferencedSet(site));

		// 将编辑页面site添加到mav中
		mav.setViewName("site/editSite");
		return mav;
	}

	@RequestMapping("/newSite")
	public ModelAndView newSite() {
		ModelAndView mav = new ModelAndView();
		Site site = new Site();

		mav.addObject("site", site);

		// 把所有的引用site对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(siteService.loadSiteObjectReferencedSet(site));

		mav.addObject("newFlag", true);
		mav.setViewName("site/editSite");

		return mav;
	}

	@RequestMapping("/saveSite")
	public ModelAndView saveSite(@ModelAttribute Site site) {

		ModelAndView mav = new ModelAndView();

		Site existingSite = CmiHibernateProxyUtil
				.deproxy(siteDAO.findSiteByPhysicalPrimaryKey(site.getName(), site.getDomain()));

		// 新建记录
		if (site.getId() == null) {
			if (site.equals(existingSite)) {

				mav.addAllObjects(siteService.loadSiteClassReferencedSet(site));
				// mav = editContinue(mav, site);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("site/editSite");
			} else {
				siteService.saveSite(site);
				mav.setViewName("forward:/listSites");
			}
		}
		// 编辑记录
		else {
			Site originSite = CmiHibernateProxyUtil.deproxy(siteDAO.findSiteByPrimaryKey(site.getId()));
			if (!site.equals(originSite) && site.equals(existingSite)) {
				mav.addAllObjects(siteService.loadSiteClassReferencedSet(site));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("site/editSite");
			} else {
				siteService.saveSite(site);
				mav.setViewName("forward:/listSites");
			}
		}
		return mav;
	}

	@RequestMapping("/siteController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
